package cspro;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.naming.InitialContext;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
/**
 * client ui
 * 
 * @author: cyj
 * @date: 2017年10月15日 下午8:36:12
 */
public class CUi extends JFrame{

	private JPanel mainPanel;
	private JPanel headPanel;
	private JPanel footPanel;
	private JTextArea showArea;
	private JTextField inputTxt;
	private JLabel portLbl;
	private JLabel ipLbl;
	private JLabel nameLbl;
	private JTextField portTxt;
	private JTextField ipTxt;
	private JTextField nameTxt;
	private JButton submitBtn;
	private JButton nameBtn;
	private JButton loginBtn;
	private Socket clientSocket = null;
	private DataOutputStream dos = null;
	private DataInputStream dis = null;
	private boolean bConnect = false;
	private String name = "";

	public static void main(String args[]){
		CUi ui = new CUi();
	}

	public CUi()
	{
		init();
	}
	private void init()
	{
		//main panel Begin
		mainPanel = new JPanel();
		showArea = new JTextArea(15, 80);
		showArea.setEditable(false);
		mainPanel.add(showArea);
		getContentPane().add(mainPanel, BorderLayout.CENTER);
		//main panel End
		//head panel Begin
		headPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
		portLbl = new JLabel("PORT");
		portTxt = new JTextField(7);
		ipLbl = new JLabel("IP");
		ipTxt = new JTextField(25);
		nameLbl = new JLabel("name");
		nameTxt = new JTextField(15);
		nameBtn = new JButton("OK");
		loginBtn = new JButton("login");
		nameBtn.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				String tmp = nameTxt.getText();
				if (tmp != null || !tmp.equals(""))
				{
					int id = javax.swing.JOptionPane.showConfirmDialog(null, "yes or no", "choose yes",
							javax.swing.JOptionPane.OK_OPTION);
					if (id == 0)
					{
						name = nameTxt.getText();
						loginBtn.setEnabled(true);
						nameBtn.setEnabled(false);
					}
				}
			}
		});
		headPanel.add(portLbl);
		headPanel.add(portTxt);
		headPanel.add(ipLbl);
		headPanel.add(ipTxt);
		headPanel.add(loginBtn);
		headPanel.add(nameLbl);
		headPanel.add(nameTxt);
		headPanel.add(nameBtn);
		loginBtn.setEnabled(true);

		loginBtn.addActionListener(new ButtonClickAction());
		getContentPane().add(headPanel, BorderLayout.NORTH);
		//head panel End

		//foot panel Begin
		footPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
		inputTxt = new JTextField(70);
		submitBtn = new JButton("submit");
		footPanel.add(inputTxt);
		footPanel.add(submitBtn);
		submitBtn.addActionListener(new ButtonClickAction());
		submitBtn.setEnabled(false);
		getContentPane().add(footPanel, BorderLayout.SOUTH);
		//foot panel End
		addWindowListener(new WindowAdapter()
		{
			public void windowClosing(WindowEvent e)
			{
				disConnect();
				System.exit(0);
			}
		});
		this.setSize(300, 300);
		setLocation(100, 100);
		pack();
		setVisible(true);
	}
	/**
	 * login button / submit button action listener
	 * 
	 * @author hx0272
	 * 
	 */
	class ButtonClickAction implements ActionListener
	{
		Socket socket = null;
		public void actionPerformed(ActionEvent e)
		{
			if ("submit".equals(e.getActionCommand()))
			{
				String str = inputTxt.getText().trim();
				inputTxt.setText("");
				sendToServer(str);
			}
			else if ("login".equals(e.getActionCommand()))
			{
				String ipAdress = ipTxt.getText().toString();
				System.out.println(portTxt.getText().toString());
				int port = Integer.parseInt(portTxt.getText().toString());
				System.out.println(ipAdress);
				socket = connect(ipAdress,port);
				receiveMsg(socket);

			}
		}
	}
	/**
	 * enter be inputted event
	 * 
	 * @author hx0272
	 */
	class EnterClickAction implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			String str = inputTxt.getText().trim();
			inputTxt.setText("");
			sendToServer(str);
		}
	}
	/**
	 * send message to server
	 * 
	 * @author hx0272
	 */
	private void sendToServer(String str)
	{
		try
		{
			dos.writeUTF(name + ":" + str);
			dos.flush();
		}
		catch(IOException e)
		{
			e.printStackTrace();
		}
	}
	/**
	 * close resource
	 * 
	 * @author hx0272
	 */
	private void disConnect()
	{
		try
		{
			// clientSocket.close();
			//          
			// dos.close();
			bConnect = false;
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	/**
	 * 接受服务器的信息 并显示
	 * @param socket
	 * @author: cyj
	 * @date: 2017年10月9日 下午1:16:30
	 */
	private void receiveMsg (Socket socket)
	{
		new Thread(
				new Runnable() {
					public void run() {
						String str = Client.receiveMsg(socket);
						showArea.setText(showArea.getText() + str + "\n");
					}
				}
				).start();
	}


	
	/**
	 * 发起对服务器的连接请求
	 * @param ipAdress
	 * @param port
	 * @return
	 * @author: cyj
	 * @date: 2017年10月9日 下午1:16:54
	 */
	private Socket connect(String ipAdress,int port)
	{
		return Client.connect(ipAdress, port);
	}

}
